package com.sudo.controller;

import com.sudo.aspect.AuthorizationApi;
import com.sudo.common.enums.MenuParams;
import com.sudo.common.utils.JsonRes;
import com.sudo.pojo.SysMenu;
import com.sudo.pojo.bo.SysMenuBO;
import com.sudo.pojo.vo.SimpleSysMenuVO;
import com.sudo.pojo.vo.SysMenuVO;
import com.sudo.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author：为道日损 2020-04-25 11:54
 */
@Api(value = "菜单接口",tags = {"菜单相关接口"})
@RestController
@RequestMapping("sysMenu")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;


    @ApiOperation(value = "查找用户菜单",notes = "查询用户的菜单给前台初始化",httpMethod = "GET")
    @GetMapping("/queryMySysMenu")
    public JsonRes queryMySysMenu(HttpServletRequest request) {
        String sessionUser = getSessionUser(request);
        List<SysMenu> sysMenuList = sysMenuService.queryMySysMenuByMailOrUsername(sessionUser);

        return JsonRes.ok(sysMenuList);
    }

    @AuthorizationApi(menu = "menuRun",button = "search")
    @ApiOperation(value = "获取所有级联菜单",notes = "后台获取级联菜单列表",httpMethod = "GET")
    @GetMapping("/getCascadeSysMenuList")
    public JsonRes getCascadeSysMenuList() {

        List<SysMenuVO> sysMenuVOList = sysMenuService.getCascadeSysMenuList();

        return JsonRes.ok(sysMenuVOList);
    }

    @AuthorizationApi(menu = "menuRun",button = "search")
    @ApiOperation(value = "根据menuCode获取菜单信息",notes = "查找一个菜单，进行修改或查看等操作",httpMethod = "GET")
    @GetMapping("/getSysMenuByCode")
    public JsonRes getSysMenuByCode(@RequestParam("menuCode") String menuCode) {
        SysMenu sysMenu = sysMenuService.getSysMenuByCode(menuCode);
        return JsonRes.ok(sysMenu);
    }

    @AuthorizationApi(menu = "menuRun",button = "search")
    @ApiOperation(value = "获取一二级菜单列表",notes = "获取一二级菜单列表，作为父菜单",httpMethod = "GET")
    @GetMapping("/getOneTwoCascadeSysMenuList")
    public JsonRes getOneTwoCascadeSysMenuList() {
        List<SysMenuVO> sysMenuVOList = sysMenuService.getOneTwoCascadeSysMenuList();
        return JsonRes.ok(sysMenuVOList);
    }

    @AuthorizationApi(menu = "menuRun",button = "update")
    @ApiOperation(value = "修改菜单信息",notes = "修改菜单信息（菜单代码不可修改）",httpMethod = "POST")
    @PostMapping("/updateSysMenuByCode")
    public JsonRes updateSysMenuByCode(@RequestBody @Valid SysMenuBO sysMenuBO,
                                     BindingResult result,
                                     HttpServletRequest request) {
        if (result.hasErrors()) {
            return JsonRes.errorMsg(getFirstError(result));
        }
        String sessionUser = getSessionUser(request);
        if (!superName(sessionUser)) {
            return JsonRes.errorMsg("只允许超级管理员操作");
        }
        if (superMenu(sysMenuBO.getmCode())) {
            return JsonRes.errorMsg("系统固有菜单，禁止修改");
        }
        //1.判断当前菜单是否能移动
        boolean isHas = sysMenuService.hasSubSysMenu(sysMenuBO.getmCode());
        boolean isMoved = sysMenuService.isMoveSysMenu(sysMenuBO.getmCode(), sysMenuBO.getpCode());
        if (isHas && isMoved) {
            return JsonRes.errorMsg("该菜单存在子菜单，不能移动");
        }

        //2.判断父级菜单是否为自自身的子菜单
        boolean isMeOrSub = sysMenuService.isSubMenuOrMyself(sysMenuBO.getmCode(),sysMenuBO.getpCode());
        if (isMeOrSub) {
            return JsonRes.errorMsg("不能移到自身或自身的子菜单下");
        }
        //3.判断父级菜单是否存在
        SysMenu sysMenu = sysMenuService.getSysMenuByCode(sysMenuBO.getpCode());
        if (StringUtils.isEmpty(sysMenu)) {
            return JsonRes.errorMsg("父级菜单不存在，不能移动");
        }
        Integer affectCount = sysMenuService.updateSysMenuByCode(sysMenuBO,sessionUser);
        return JsonRes.ok(affectCount);
    }

    @AuthorizationApi(menu = "menuRun",button = "add")
    @ApiOperation(value = "新增菜单",notes = "新增菜单",httpMethod = "POST")
    @PostMapping("/addSysMenu")
    public JsonRes addSysMenu(@RequestBody @Valid SysMenuBO sysMenuBO,
                                     BindingResult result,
                                     HttpServletRequest request) {
        if (result.hasErrors()) {
            return JsonRes.errorMsg(getFirstError(result));
        }
        String sessionUser = getSessionUser(request);
        if (!superName(sessionUser)) {
            return JsonRes.errorMsg("只允许超级管理员操作");
        }
        //0.判断mCode在数据库中是否已存在
        if (sysMenuService.hasSysMenu(sysMenuBO.getmCode())) {
            return JsonRes.errorMsg("该菜单代码已存在");
        }
        //1.判断所选父级菜单是否存在
        SysMenu sysMenu = sysMenuService.getSysMenuByCode(sysMenuBO.getpCode());
        if (StringUtils.isEmpty(sysMenu)) {
            return JsonRes.errorMsg("当前父级菜单不存在");
        }
        Integer affectCount = sysMenuService.addSysMenu(sysMenuBO,sessionUser);
        return JsonRes.ok(affectCount);
    }

    @AuthorizationApi(menu = "menuRun",button = "delete")
    @ApiOperation(value = "删除菜单",notes = "删除菜单",httpMethod = "POST")
    @PostMapping("/deleteSysMenuByCode")
    public JsonRes deleteSysMenuByCode(@RequestParam("menuCode") String menuCode,
                              HttpServletRequest request) {
        String sessionUser = getSessionUser(request);
        if (!superName(sessionUser)) {
            return JsonRes.errorMsg("只允许超级管理员操作");
        }
        if (superMenu(menuCode)) {
            return JsonRes.errorMsg("系统固有菜单，禁止删除");
        }
        boolean flag = sysMenuService.hasSubSysMenu(menuCode);
        if (flag) {
            return JsonRes.errorMsg("删除失败，该菜单下存在子菜单");
        }
        Integer affectCount = sysMenuService.deleteSysMenuByCode(menuCode);
        return JsonRes.ok(affectCount);
    }
}
